package com.woniuxy.charin.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.model.entity.GroupInfo;
import com.woniuxy.charin.service.GroupInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/groupInfo")
@Api(tags = "组团管理")
@Slf4j
public class GroupInfoController {

    @Autowired
    private GroupInfoService groupInfoService;

    @GetMapping("/getGroupInfoByPage")
    @ApiOperation("分页查询组团信息")
    public ResponseEntity<Page<GroupInfo>> getGroupInfoByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String groupName,
            @RequestParam(required = false) String groupStatus,
            @RequestParam(required = false) String groupType) {

        log.info("分页查询组团信息, page={}, size={}, groupName={}, groupStatus={}, groupType={}",
                page, size, groupName, groupStatus, groupType);

        Page<GroupInfo> result = groupInfoService.findGroupInfos(page, size, groupName, groupStatus, groupType);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/addGroupInfo")
    @ApiOperation("添加组团信息")
    public ResponseEntity<String> addGroupInfo(@RequestBody GroupInfo groupInfo) {
        log.info("添加组团信息: {}", groupInfo);
        groupInfo.setPublishTime(new Date());  // 设置发布时间
        boolean result = groupInfoService.save(groupInfo);
        return ResponseEntity.ok(result ? "添加成功" : "添加失败");
    }

    @PutMapping("/updateGroupInfo")
    @ApiOperation("更新组团信息")
    public ResponseEntity<String> updateGroupInfo(@RequestBody GroupInfo groupInfo) {
        log.info("更新组团信息: {}", groupInfo);
        boolean result = groupInfoService.updateById(groupInfo);
        return ResponseEntity.ok(result ? "更新成功" : "更新失败");
    }

    @DeleteMapping("/deleteGroupInfo")
    @ApiOperation("删除组团信息")
    public ResponseEntity<String> deleteGroupInfo(@RequestParam Integer id) {
        log.info("删除组团信息, id={}", id);
        boolean result = groupInfoService.removeById(id);
        return ResponseEntity.ok(result ? "删除成功" : "删除失败");
    }

    @GetMapping("/getGroupInfoById")
    @ApiOperation("根据ID查询组团信息")
    public ResponseEntity<GroupInfo> getGroupInfoById(@RequestParam Integer id) {
        log.info("查询组团信息, id={}", id);
        GroupInfo groupInfo = groupInfoService.getById(id);
        return ResponseEntity.ok(groupInfo);
    }

    @PutMapping("/recycleGroupInfo")
    @ApiOperation("回收组团（更新状态为已结束）")
    public ResponseEntity<String> recycleGroupInfo(@RequestParam Integer id) {
        log.info("回收组团信息, id={}", id);

        GroupInfo groupInfo = groupInfoService.getById(id);
        if (groupInfo != null) {
            groupInfo.setGroupStatus("已结束");
            boolean result = groupInfoService.updateById(groupInfo);
            return ResponseEntity.ok(result ? "回收成功" : "回收失败");
        }
        return ResponseEntity.ok("组团不存在");
    }

    @PutMapping("/publish")
    @ApiOperation("发布组团（更新状态为进行中）")
    public ResponseEntity<String> publish(@RequestParam int id) {
        log.info("publish - id: {}", id);

        GroupInfo groupInfo = groupInfoService.getById(id);
        if (groupInfo != null) {
            groupInfo.setGroupStatus("进行中");
            boolean updated = groupInfoService.updateById(groupInfo);
            return ResponseEntity.ok(updated ? "组团已发布（状态进行中）" : "发布失败");
        } else {
            return ResponseEntity.badRequest().body("组团不存在");
        }
    }


}

